package com.example.mall.controller;


import com.example.mall.common.ApiResponse;
import com.example.mall.entity.User;
import com.example.mall.service.UserService;
import com.example.mall.so.LoginSo;
import com.example.mall.so.RegisterSo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public ApiResponse register(@Valid @RequestBody RegisterSo registerSo){
        if (registerSo.getPassword().equals(registerSo.getCheckPassword())==false){
            return new ApiResponse(false, "两次密码不一致");
        }
        return userService.register(registerSo.getUsername(), registerSo.getPassword());
    }

    @PostMapping("/login")
    public ApiResponse login(@Valid @RequestBody LoginSo loginSo){
        return userService.login(loginSo.getUsername(), loginSo.getPassword());
    }
}
